﻿using Abp.Runtime.Session;
using Abp.Timing.Timezone;
using Camc.Quality.DataExporting.Excel.EpPlus;
using Camc.Quality.Dto;
using Camc.Quality.Storage;
using Camc.Quality.Tooling.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Camc.Quality.Tooling.ToolingInterface
{
   public class ToolingExportToExcel : EpPlusExcelExporterBase, IToolingsExprotToExcel
    {
        private readonly ITimeZoneConverter _timeZoneConverter;
        private readonly IAbpSession _abpSession;

        public ToolingExportToExcel(
            ITimeZoneConverter timeZoneConverter,
            IAbpSession abpSession,
            ITempFileCacheManager tempFileCacheManager)
            : base(tempFileCacheManager)
        {
            _timeZoneConverter = timeZoneConverter;
            _abpSession = abpSession;
        }
        public string ExportToFile(List<ToolingContentDto> materialsContentDtos)
        {
            return CreateExcelPackageLocal(
                "工装夹具明细.xlsx",
                excelPackage =>
                {
                    var sheet = excelPackage.Workbook.Worksheets.Add(L("工装夹具明细"));
                    sheet.OutLineApplyStyle = true;
                    AddHeader(
                        sheet,
                        L("工装编号"),
                        L("工装名称"),
                        L("工装类别"),
                        L("单位"),
                        L("使用状态"),
                        L("规格型号"),
                        L("上次校验日期"),
                        L("校验周期/天"),
                        L("寿命状态"),
                        L("保管员"),
                        L("等级"),
                        L("产品图号"),
                        L("备注"),
                        L("创建人"),
                        L("创建时间")
                        );

                    AddObjects(
                        sheet, 2, materialsContentDtos,
                        _ => _.ToolingNo,
                        _ => _.ToolingName,
                        _ => _.ToolingTypeName,
                        _ => _.Unit,
                        _ => _.ToolIsUseState,
                        _ => _.Modle,
                        _ => _.PreviousVerifyTime,
                        _ => _.CheckCycleTime,
                        _ => _.LifeStateName,
                        _ => _.KeeperName,
                        _ => _.LevelNo,
                        _ => _.DrawingNo,
                        _ =>_.Remark,
                        _ =>_.CreateUserName,
                        _ => _timeZoneConverter.Convert(_.CreationTime, _abpSession.TenantId, _abpSession.GetUserId())
                        );
                    //Formatting cells
                    var creationTimeColumn = sheet.Column(15);
                    creationTimeColumn.Style.Numberformat.Format = "yyyy-mm-dd HH:mm:ss";

                    for (var i = 1; i <= 9; i++)
                    {
                        sheet.Column(i).AutoFit();
                    }
                });
        }
    }
}
